package com.example.admin.goodnews.fragment.ShengHuoChangYong;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.JinEDaXieDataEntity;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

/**
 * 金额大写页面
 * Created by admin on 2016/10/15.
 */

public class JinEDaXieFragment extends Fragment {
    private TextInputLayout til_jinedaxie;
    private EditText et_jinedaxie;// 接收输入的内容
    private TextView tv_jinedaxie;// 显示输入的内容
    private TextView tv_change_jinedaxie;// 显示转换后的内容
    private Button btn_change;// 点击转换
    private Button btn_reset;// 点击清空
    private JinEDaXieDataEntity jinEDaXieDataEntity;// 数据实体类
    String time = GetCurrentTimeUtil.getCurrentTime();// 获取当前系统时间
    String number = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_jinedaxie, null);

        til_jinedaxie = (TextInputLayout) view.findViewById(R.id.til_jinedaxie);
        et_jinedaxie = til_jinedaxie.getEditText();
        til_jinedaxie.setHint("请输入金额");

        TextChange textChange = new TextChange();
        et_jinedaxie.addTextChangedListener(textChange);

        tv_jinedaxie = (TextView) view.findViewById(R.id.tv_jinedaxie);
        tv_change_jinedaxie = (TextView) view.findViewById(R.id.tv_change_jinedaxie);
        btn_change = (Button) view.findViewById(R.id.btn_change);
        btn_reset = (Button) view.findViewById(R.id.btn_reset);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 按钮点击事件
        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_jinedaxie.setText("");
                tv_jinedaxie.setText("输入的金额：");
                tv_change_jinedaxie.setText("转换后：");
            }
        });
    }

    private void getData() {
        String param = NetWorkDataUrl.JINEDAXIE_PARAM1 + number + NetWorkDataUrl.JINEDAXIE_PARAM2 + time + NetWorkDataUrl.JINEDAXIE_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.JINEDAXIE_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    jinEDaXieDataEntity = gson.fromJson(result, JinEDaXieDataEntity.class);
                    if (jinEDaXieDataEntity.getShowapi_res_body() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    tv_jinedaxie.setText("输入的金额：" + number);
                    tv_change_jinedaxie.setText("转换后：" + jinEDaXieDataEntity.getShowapi_res_body().getDaxie());
                    break;
            }
        }
    };

    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            number = et_jinedaxie.getText().toString();
            if (number.length() > 0) {
                btn_change.setEnabled(true);
            } else {
                btn_change.setEnabled(false);
            }
            if (number.length() > 0) {
                btn_reset.setEnabled(true);
            } else {
                btn_reset.setEnabled(false);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {
            number = et_jinedaxie.getText().toString();
        }
    }

}
